    function [C] = Consumption_HHW(Cg,iX)

    Nstates = size(Cg,1)-1;
    T = size(iX,1);
    nInd = size(iX,2);
    Cs = Cg(:,1);
    Csi = Cs(iX(1,:))';

    Cgri = Csi;
    C = zeros(T,nInd);
    C(1,:) =  Csi;
    Stay = ones(T,nInd);

    for t = 2:T
       Cs = Cg(:,t);
       Csi = Cs(iX(t,:))'; 
       % drop if dead:
       stay = (Cgri>=Csi).*(iX(t,:)~=(Nstates+1));
       Cgri = Cgri.*stay + Csi.*(1-stay);
       C(t,:) = Cgri; 
       Stay(t,:) = stay;
    end

end